package com.example.bloodbankapplication.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.util.Log;

import com.example.bloodbankapplication.entity.PersonInformationModel;

public class DonorStorage {
	private SQLiteDatabase sb;
	
	public DonorStorage(Context context){
		OpenHelper helper = new OpenHelper(context);
		sb = helper.getWritableDatabase();
	}

	public long addRecentViwedDonor(Bundle bundle) {
		try{
			ContentValues values = new ContentValues();
			values.put("firstname", bundle.getString("Name"));
			values.put("phno", bundle.getString("PHNO"));
			values.put("bloodgroup", bundle.getString("BG"));
			values.put("lastname", bundle.getString("LastName"));
			values.put("emailid", bundle.getString("Email"));
			values.put("date", bundle.getString("Date"));
			return sb.insert("Donor", null, values);
		}catch(Exception ex){
			return 0;
		}
	}

	public ArrayList<PersonInformationModel> getViewedDonorList() {
		ArrayList<PersonInformationModel> list = new ArrayList<PersonInformationModel>();
		String columns[] ={"firstname","lastname","bloodgroup","emailid","phno"};
		Cursor cursor = sb.query("Donor", columns, null, null, null, null, null);
		if(cursor.moveToFirst()){
			do{
				PersonInformationModel model = new PersonInformationModel();
				model.setName(cursor.getString(0));
				model.setLastname(cursor.getString(1));
				model.setPhno(cursor.getString(2));
				model.setBloodGroup(cursor.getString(3));
				model.setEmail(cursor.getString(4));
				list.add(model);
			}while(cursor.moveToNext());
				
			
		}
		
		return list;
	}
	
	public void deleteOldDonor(){
		
			 SQLiteStatement stmt  = null;              
			try{
				stmt = sb.compileStatement("DELETE FROM Donor WHERE date in (select date from Donor limit 1)");
				Log.i("NCS","in delete");                                      
			}catch (Exception e) {         
				Log.i("NCS","Exception is  :"+e.getMessage());                                            
			}
			 stmt.execute();                                                                    
	}

}
